package com.th.supcom.pds.phamapply.server;

import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.common.client.model.PtsPaiAdtLogModel;
import com.th.supcom.pds.domain.pojo.InpPhamApplyDetail;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.PrescSearchDTOVO;
import com.th.supcom.pds.internal.api.phamapply.NurseReturnService;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyPatInfoModel;
import com.th.supcom.pds.phamapply.client.model.PrescSearchDTOModel;
import com.th.supcom.pds.phamapply.client.service.NurseReturnRpc;

/**
 * NurseReturn 控制层.
 * 
 * 护士站退药.
 */
public class NurseReturnServiceController extends GwtController implements NurseReturnRpc
{
    private NurseReturnService m_nurseReturnService;
    private static final long serialVersionUID = -5589159575983246719L;

    /**
     * 默认构造函数.
     */
    public NurseReturnServiceController ()
    {

    }

    /**
     * 输入患者ID查询患者就诊次数
     */
    @Override
    public List <InpPhamApplyPatInfoModel> findPatInfoByPatientId (String patientId) throws UiException
    {
        s_logger.debug ("调用findPatInfoByPatientId方法查询患者就诊记录:patientId{}", new Object[]
        { patientId });
        List <InpPhamApplyPatInfoModel> models;
        try
        {
            List <InpPhamApplyPatInfo> inpPhamApplyPatInfo = m_nurseReturnService.findPatientInfoByPatient (Long.parseLong (patientId));
            models = ConvertUtils.convert (InpPhamApplyPatInfoModel.class, inpPhamApplyPatInfo);
            for (InpPhamApplyPatInfoModel model : models)
            {
                model.setWatchTimes ("第" + model.getVisitId () + "次住院");
            }
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
        return models;
    }

    /**
     * 输入患者流水号，查询患者某次住院所有转科记录
     */
    @Override
    public List <PtsPaiAdtLogModel> findInPatientAdtLogInfoById (Long paiVisitId) throws UiException
    {
        s_logger.debug ("调用findInPatientAdtLogInfoById方法查询患者某次住院所有转科记录:patientId{}", new Object[]
        { paiVisitId });
        List <PtsPaiAdtLogModel> ptsPaiAdtLogModels = ConvertUtils.convert (PtsPaiAdtLogModel.class, m_nurseReturnService.findPaiAdtLogVosById (paiVisitId));
        return ptsPaiAdtLogModels;
    }

    /**
     * 根据 患者相关信息查询患者就诊明细
     * 
     * @param prescSearchDTOModel 查询条件
     * @return 获取患者计费明细
     * @throws UiException
     */
    @Override
    public List <InpPhamApplyDetailModel> findChargeItemByExample (PrescSearchDTOModel prescSearchDTOModel) throws UiException
    {
        s_logger.debug ("调用findChargeItemByExample方法查询患者就诊费用明细:prescSearchDTOModel【{}】", new Object[]
        { prescSearchDTOModel });
        PrescSearchDTOVO prescSearchDTOVO = ConvertUtils.convert (PrescSearchDTOVO.class, prescSearchDTOModel);
        prescSearchDTOVO.setStartDate (prescSearchDTOModel.getStartDate ());
        prescSearchDTOVO.setEndDate (prescSearchDTOModel.getEndDate ());
        try
        {
            List <InpPhamApplyDetailModel> inpPhamApplyDetailModels = ConvertUtils.convert (InpPhamApplyDetailModel.class, m_nurseReturnService.findChargeItemByExample (prescSearchDTOVO));
            for (InpPhamApplyDetailModel inpPhamApplyDetailModel : inpPhamApplyDetailModels)
            {
                if (inpPhamApplyDetailModel.getWithdrawType () == null)
                {
                    inpPhamApplyDetailModel.setWithdrawType ("N");
                    inpPhamApplyDetailModel.setWithdrawTypeName ("无药退药");
                }
            }
            return inpPhamApplyDetailModels;
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    public void setNurseReturnService (NurseReturnService nurseReturnService)
    {
        m_nurseReturnService = nurseReturnService;
    }

    /**
     * 生成电子退药单
     * 
     * @param inpPhamApplyDetailModels
     * @param operator
     * @param generateFlag true: 生成电子退药单，false: 生成退药处方
     * @throws UiException
     */
    @Override
    public void generateBill (List <InpPhamApplyDetailModel> inpPhamApplyDetailModels, String nurseUnit, String operator, InpPhamApplyPatInfoModel inpPhamApplyPatInfoModel) throws UiException
    {
        s_logger.debug ("调用generateBill方法生成费用退药单:inpPhamApplyDetailModels:【{}】,nurseUnit:{},operator:{},inpPhamApplyPatInfoModel:{}", new Object[]
        { inpPhamApplyDetailModels, nurseUnit, operator, inpPhamApplyPatInfoModel });
        List <InpPhamApplyDetail> inpPhamApplyDetails = ConvertUtils.convert (InpPhamApplyDetail.class, inpPhamApplyDetailModels);
        InpPhamApplyPatInfo inpPhamApplyPatInfo = ConvertUtils.convert (InpPhamApplyPatInfo.class, inpPhamApplyPatInfoModel);
        try
        {
            m_nurseReturnService.generateDrugReturn (inpPhamApplyDetails, nurseUnit, operator, inpPhamApplyPatInfo);
        }
        catch (Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public InpPhamApplyPatInfoModel getPatInfoByBedNo (String bedNo, String nurseUnitCode) throws UiException
    {
        s_logger.debug ("调用getPatInfoByBedNo方法通过床号查询患者信息:bedNo:{},nurseUnitCode:{}", new Object[]
        { bedNo, nurseUnitCode });
        InpPhamApplyPatInfo inpPhamApplyPatInfo;
        try
        {
            inpPhamApplyPatInfo = m_nurseReturnService.getPatInfoByBedNo (nurseUnitCode, bedNo);
            InpPhamApplyPatInfoModel inpPhamApplyPatInfoModel = ConvertUtils.convert (InpPhamApplyPatInfoModel.class, inpPhamApplyPatInfo);
            return inpPhamApplyPatInfoModel;
        }
        catch (Exception e)
        {
            throw new UiException (e.getLocalizedMessage (), e);
        }

    }

    private static final Logger s_logger = LoggerFactory.getLogger (NurseReturnServiceController.class);
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}